package com.example.ywj.toutiao;

import android.content.Intent;
import android.graphics.Typeface;
import android.os.Build;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.TextView;

import com.example.ywj.toutiao.utils.Constant;
import com.example.ywj.toutiao.utils.ResponseApi;
import com.example.ywj.toutiao.utils.SpUtils;
import com.example.ywj.toutiao.utils.Util;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

import java.io.IOException;

import butterknife.BindView;
import butterknife.ButterKnife;
import es.dmoral.toasty.Toasty;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class RegisterPasswordActivity extends AppCompatActivity implements View.OnClickListener {
    private static final String TAG = "RegisterPasswordActivit";
    @BindView(R.id.password_back2)
    public TextView password_back2;
    @BindView(R.id.password_next2)
    public TextView password_next2;
    @BindView(R.id.password1)
    public EditText password1;
    @BindView(R.id.password2)
    public EditText password2;
    Boolean flag = false;
    public String username;
    private String baseUrl = Constant.BASEURL;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register_password);
        ButterKnife.bind(this);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {//因为不是所有的系统都可以设置颜色的，在4.4以下就不可以。。有的说4.1，所以在设置的时候要检查一下系统版本是否是4.1以上
            Window window = getWindow();
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(getResources().getColor(R.color.trgray));
        }
        Intent intent = getIntent();
        username = intent.getStringExtra("username");
        initView();
    }
    public void initView(){
        final Typeface font = Typeface.createFromAsset(getAssets(), "iconfont.ttf");
        password_back2.setTypeface(font);
        password_next2.setTypeface(font);
        password_back2.setText(getResources().getString(R.string.back));
        password_next2.setText(getResources().getString(R.string.next));
        password_back2.setOnClickListener(this);
        password_next2.setOnClickListener(this);
        password1.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if(Util.checkPwd(String.valueOf(s))){
                    flag = true;
                }else{
                    flag = false;
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        password2.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if(Util.checkPwd(String.valueOf(s))&&password1.getText().toString().equals(password2.getText().toString())){
                    flag = true;
                    password_next2.setBackground(getDrawable(R.drawable.border_textview2));
                    password_next2.setTextColor(getColor(R.color.white));
                }else{
                    password_next2.setBackground(getDrawable(R.drawable.border_textview));
                    flag = false;
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }
    @Override
    protected void onPause() {
        overridePendingTransition(0,0);
        super.onPause();
    }
    public void register(String username,String password){
        Retrofit retrofit = new Retrofit.Builder()
                .addConverterFactory(GsonConverterFactory.create())
                .baseUrl(baseUrl)
                .build();
        ResponseApi responseApi = retrofit.create(ResponseApi.class);
        Call<ResponseBody> call = responseApi.register(username,password);
        call.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                String json = null;
                try {
                    json = response.body().string();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                Log.i(TAG, "onResponse: "+json);
                JsonObject obj = (JsonObject) new JsonParser().parse(json);
                Integer code = obj.get("statusCode").getAsInt();
                if(code == 200 && obj.has("data")){
                    JsonObject objArr = obj.getAsJsonObject("data");
                    Log.i(TAG, "onResponse: "+objArr.get("username").getAsString());
                    String username = objArr.get("username").getAsString();
                    SpUtils.putString(RegisterPasswordActivity.this,"username",username);
                    SpUtils.putBoolean(RegisterPasswordActivity.this,"isreturn",false);
                    Intent intent = new Intent(RegisterPasswordActivity.this,MainActivity.class);
                    startActivity(intent);
                    finish();
                }
            }
            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
                Log.i(TAG, "onFailure: "+t);
            }
        });
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.password_back2:
                finish();
                break;
            case R.id.password_next2:
                if(flag && password1.getText().toString().equals(password2.getText().toString())){
                    register(username,password1.getText().toString());
                    flag = false;
                }else if(!flag){
                    Toasty.info(RegisterPasswordActivity.this,"请输入6-16位数字,字母,特殊字符中两者的密码",Toasty.LENGTH_LONG).show();
                    if(!password1.getText().toString().equals(password2.getText().toString())){
                        Toasty.error(RegisterPasswordActivity.this,"密码不一致",Toasty.LENGTH_LONG).show();
                    }
                }
                break;
        }
    }
}
